Using NYC Geo

hydrant <- 
  violation %>% 
  filter(violation %in% c("FIRE HYDRANT"), !is.na(geo_nyc_address))

pb <- progress_bar$new(total = nrow(hydrant))

get_coord <- function(url) {
  pb$tick()
  coord <- fromJSON(url(url))$features$geometry[2]
  return(coord)
}

hydrant_lat_long <- 
  hydrant %>%
  mutate(
  new_url = paste0("https://geosearch.planninglabs.nyc/v1/autocomplete?text=", geo_nyc_address, "&size=50"),
  coord = map(new_url, get_coord)
) %>%
  unnest(coord)

for (i in 1:nrow(hydrant_lat_long)) {
  hydrant_lat_long$lat[i] <- hydrant_lat_long$coordinates[[i]][2]
  hydrant_lat_long$long[i] <- hydrant_lat_long$coordinates[[i]][1]
}

hydrant_lat_long <-
  hydrant_lat_long %>%
  group_by(summons_number, geo_nyc_address) %>%
  slice(1)
  
write_csv(hydrant_lat_long, "hydrant_lat_long.csv")
hydrant_lat_long <- read_csv("hydrant_lat_long.csv") %>% select(-coordinates)
## Rows: 112874 Columns: 33
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr  (16): plate, state, license_type, min, violation, precinct, borough, is...
## dbl  (14): summons_number, hour, fine_amount, penalty_amount, interest_amoun...
## lgl   (2): judgment_entry_date, coordinates
## date  (1): issue_date
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.
hydrant_actual <- read_csv("Hydrants.csv")
## Rows: 109526 Columns: 9
## -- Column specification --------------------------------------------------------
## Delimiter: ","
## chr (2): the_geom, UNITID
## dbl (7): OBJECTID, BORO, POINT_X, POINT_Y, CB, LATITUDE, LONGITUDE
## 
## i Use `spec()` to retrieve the full column specification for this data.
## i Specify the column types or set `show_col_types = FALSE` to quiet this message.

Mapping of Hydrant Tickets

hydrant_plot <- 
  hydrant_lat_long %>% 
  plot_ly(
    lat = ~lat, 
    lon = ~long, 
    type = "scattermapbox", 
    mode = "markers", 
    alpha = 0.1)

hydrant_plot %>% layout(
    mapbox = list(
      style = 'carto-positron',
      zoom = 9,
      center = list(lon = -73.9, lat = 40.7)))